package com.groupten.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupten.Dao.QuestionDao;
import com.groupten.Entity.QuestionEntity;
import com.groupten.Service.QuestionService;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("questionService")
public class QuestionServiceImpl extends ServiceImpl<QuestionDao,QuestionEntity> implements QuestionService {

    @Override
    public Integer insertQuestion(QuestionEntity question) {
        return baseMapper.insert(question);
    }

    @Override
    public Integer deleteQuestionById(Long QuestionId) {
        LambdaQueryWrapper<QuestionEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(QuestionId), QuestionEntity::getQuestionId, QuestionId);
        return baseMapper.delete(queryWrapper);
    }

    @Override
    public IPage<QuestionEntity> selectAll(Integer page, Integer limit) {
        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<QuestionEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, Wrappers.lambdaQuery());
    }


    @Override
    public QuestionEntity selectQuestionByID(Long QuestionID) {
        LambdaQueryWrapper<QuestionEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(QuestionID), QuestionEntity::getQuestionId, QuestionID);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<QuestionEntity> selectQuestionByType(Integer Type, Integer page, Integer limit) {
        LambdaQueryWrapper<QuestionEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(Type),QuestionEntity::getQuestionType, Type);

        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<QuestionEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, queryWrapper);
    }

    @Override
    public IPage<QuestionEntity> selectQuestionBySubject(Integer Subject, Integer page, Integer limit) {
        LambdaQueryWrapper<QuestionEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(Subject),QuestionEntity::getSubject, Subject);

        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<QuestionEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, queryWrapper);
    }

    @Override
    public IPage<QuestionEntity> selectQuestionByKnowledgePoint(Integer KnowledgePoint, Integer page, Integer limit) {
        LambdaQueryWrapper<QuestionEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(KnowledgePoint),QuestionEntity::getKnowledgePoint, KnowledgePoint);

        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<QuestionEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, queryWrapper);
    }

    @Override
    public IPage<QuestionEntity> selectQuestionByScore(Double Score, Integer page, Integer limit) {
        LambdaQueryWrapper<QuestionEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(Score),QuestionEntity::getScore, Score);

        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<QuestionEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, queryWrapper);
    }

    @Override
    public IPage<QuestionEntity> selectQuestionByLevel(Double Level, Integer page, Integer limit) {
        LambdaQueryWrapper<QuestionEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(Level),QuestionEntity::getLevel, Level);

        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<QuestionEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, queryWrapper);
    }



    @Override
    public boolean removeDuplicateQuestion(QuestionEntity question) {
        List<QuestionEntity> allQuestions = baseMapper.selectList(null); // 获取所有问题
        Map<String, Long> uniqueQuestions = new HashMap<>();

        for (QuestionEntity q : allQuestions) {
            String questionText = q.getQuestionText();
            String prefix = questionText.length() > 10 ? questionText.substring(0, 10) : questionText;

            // 检查是否存在相同前缀的问题
            if (uniqueQuestions.containsKey(prefix)) {
                // 如果存在相同前缀的问题，返回 false
                return false;
            } else {
                // 如果不存在相同前缀的问题，记录当前问题的前缀和 ID
                uniqueQuestions.put(prefix, q.getQuestionId());
            }
        }

        // 检查当前问题是否与已存在的问题重复
        String currentPrefix = question.getQuestionText().length() > 10 ? question.getQuestionText().substring(0, 10) : question.getQuestionText();
        if (uniqueQuestions.containsKey(currentPrefix)) {
            return false;
        }

        // 如果当前问题不重复，返回 true
        return true;
    }

    @Override
    public IPage<QuestionEntity> selectQuestionBySubjectAndType(Integer subject, Integer type, Integer page, Integer limit) {
        LambdaQueryWrapper<QuestionEntity> queryWrapper = new LambdaQueryWrapper<QuestionEntity>()
                .eq(subject!=null, QuestionEntity::getSubject, subject)
                .eq(type!=null, QuestionEntity::getQuestionType, type);
        int curPage = 1;
        int curLimit = 10;
        if (page != null && page > 0){
            curPage = page;
        }
        if (limit != null && limit > 0){
            curLimit = limit;
        }
        return baseMapper.selectPage(new Page<>(curPage, curLimit), queryWrapper);
    }

    @Override
    public IPage<QuestionEntity> selectQuestionByKnowledgePointAndType(Integer knowledgePoint, Integer type, Integer page, Integer limit) {
        LambdaQueryWrapper<QuestionEntity> queryWrapper = new LambdaQueryWrapper<QuestionEntity>()
                .eq(knowledgePoint!=null, QuestionEntity::getKnowledgePoint, knowledgePoint)
                .eq(type!=null, QuestionEntity::getQuestionType, type);
        int curPage = 1;
        int curLimit = 10;
        if (page != null && page > 0){
            curPage = page;
        }
        if (limit != null && limit > 0){
            curLimit = limit;
        }
        return baseMapper.selectPage(new Page<>(curPage, curLimit), queryWrapper);
    }

    @Override
    public IPage<QuestionEntity> convertListtoIPage(List<QuestionEntity> list, Integer page, Integer limit) {
        IPage<QuestionEntity> pageObject = new Page<>(page,limit);
        pageObject.setTotal(list.size());
        long fromIndex = pageObject.getCurrent() > 1 ? (pageObject.getCurrent() - 1) * pageObject.getSize() : 0;
        long toIndex = fromIndex + pageObject.getSize() > list.size() ? list.size() : fromIndex + pageObject.getSize();
        pageObject.setRecords(list.subList((int) fromIndex, (int) toIndex));
        //重新设置一下当前页的数据个数
        pageObject.setSize(pageObject.getRecords().size());
        return pageObject;
    }

}
